package com.wow.app.promotion.web.personal.module.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.wow.app.biz.common.SessionUser;
import com.wow.app.biz.common.SessionUtil;
import com.wow.app.biz.page.PageService;
import com.wow.app.dal.dto.PageDO;

public class PageAction {
	@Resource
	PageService pageService;

	public void doAddPage(@Param("title") String title,
			@Param("content") String content, HttpSession session,
			Navigator nav, Context context) {
		PageDO pageDO = new PageDO();
		pageDO.setContent(content);
		pageDO.setTitle(title);
		SessionUser sessionUser = SessionUtil.getSessionUser(session);
		pageDO.setOwner(sessionUser.getLoginID());
		pageService.addPage(pageDO);
		nav.redirectTo("pageListLink");
	}

	public void doUpdatePage(@Param("title") String title,
			@Param("content") String content, HttpSession session,
			Navigator nav, Context context) {
		PageDO pageDO = new PageDO();
		pageDO.setContent(content);
		pageDO.setTitle(title);
		SessionUser sessionUser = SessionUtil.getSessionUser(session);
		pageDO.setOwner(sessionUser.getLoginID());
		pageService.updatePage(pageDO);
		nav.redirectTo("pageListLink");
	}

	public void doDeletePage(@Param("id") String id, HttpSession session,
			Navigator nav) {
		if (id != null) {
			SessionUser sessionUser = SessionUtil.getSessionUser(session);
			PageDO pageDO = pageService.getPageByID(Integer.parseInt(id));
			if (pageDO != null
					&& pageDO.getOwner().equals(sessionUser.getLoginID())) {
				pageService.deletePage(Integer.parseInt(id));
				nav.redirectTo("pageListLink");
			}
		} else {
			nav.redirectTo("errorLink");
		}
	}
}